<?php
namespace app\home\controller;

use think\Controller;
use think\Db;
use think\Session;

class Gives extends Controller
{
    //添加点赞
    public function getadd()
    {
        if(!empty(Session::get('userinfo')['user_id'])) {
            $resquest=$this->request;
            $res=$resquest->except('action');
            $res['goods_id']=$res['id'];
            $res['user_id']=Session::get('userinfo')['user_id'];
            $res['create_time']=time();
            unset($res['id']);
            $ress=Db::table('tp_gives')->where('user_id',$res['user_id'])
                ->where('goods_id',$res['goods_id'])->find();
            if($ress)
            {
                $data=[
                    'status' => 1,
                    'msg' => '你已经点赞过该商品，不能重复点赞该商品',
                ];
                return $data;
            }
            $result=Db::table('tp_gives')->insert($res);
            if($result){
                //返回数据，进行判断是否添加成功
                $data=[
                    'status' => 0,
                    'msg' => '点赞成功',
                ];
            }else{
                $data=[
                    'status' => 1,
                    'msg' => '点赞失败',
                ];
            };
            return json($data);
        }else{
            $data=[
                'status' => 1,
                'msg' => '请登录',
            ];
            return $data;
        }

    }

    public function getIndex()
    {
        $user_id=Session::get('userinfo')['user_id'];
        $goods_datas=Db::table('tp_gives')->alias('tgs')
            ->field('tgs.id,tgs.goods_id,tg.goods_price,tg.id,tg.goods_name,goods_pic')
            ->join('tp_goods tg','tgs.goods_id=tg.id')
            ->where('user_id',$user_id)
            ->select();
        $this->assign('goods_datas',$goods_datas);
        return $this->fetch('gives/index');
    }
}
?>